package com.young.monitor.service.impl;

import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.exception.BaseException;
import com.young.common.util.DateUtil;
import com.young.common.util.Page;
import com.young.common.util.StringUtils;
import com.young.monitor.model.ApiIndex;
import com.young.monitor.model.ApiIndexStatSearch;
import com.young.monitor.model.TimeDimension;
import com.young.monitor.service.IApiIndexService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* api监控指标服务实现
* Created by imrookie on 2019-7-2.
*/
@Service("apiIndexService")
public class ApiIndexServiceImpl implements IApiIndexService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(ApiIndexServiceImpl.class);

    private static final String MAPPER = "com.young.monitor.mapper.apiIndex";//mapper的namespace

    //获取数据列表
    @Override
    public List<ApiIndex> getList(ApiIndex obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Override
    public List<ApiIndex> getPage(ApiIndex obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Override
    public ApiIndex get(Long id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    //统计数量
    @Override
    public int count(ApiIndex obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(ApiIndex obj) {
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(Long[] idArr) {
        int i = 0;
        for(Long id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(Long id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(ApiIndex obj) {
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<ApiIndex> list) {
        int i = 0;
        for(ApiIndex item : list){
            i += this.insert(item);
        }
        return i;
    }

    /**
     * 获取接口指标统计数据
     *
     * @param apiIndexStatSearch 统计条件
     * @return
     */
    @Override
    public List<ApiIndex> getApiIndexStat(ApiIndexStatSearch apiIndexStatSearch) {
        if (StringUtils.isBlank(apiIndexStatSearch.getApp()) || StringUtils.isBlank(apiIndexStatSearch.getKey())) {
            throw new BaseException("应用名或监控KEY为空");
        }
//        if (apiIndexStatSearch.getTimeRange() == null) {
//            throw new BaseException("时间范围为空");
//        }
        if (apiIndexStatSearch.getDimension() == null) {
            throw new BaseException("时间查询维度为空");
        }
        if (apiIndexStatSearch.getStartTime() == null) {
            throw new BaseException("开始时间为空");
        }
        if (apiIndexStatSearch.getEndTime() == null) {
            throw new BaseException("结束时间为空");
        }

        apiIndexStatSearch.setStartMinutes(Long.parseLong(DateUtil.dateFormat(apiIndexStatSearch.getStartTime(), "yyyyMMddHHmm")));
        apiIndexStatSearch.setEndMinutes(Long.parseLong(DateUtil.dateFormat(apiIndexStatSearch.getEndTime(), "yyyyMMddHHmm")));
        return dataAccessService.getList(MAPPER + ".statMonitorByKey", apiIndexStatSearch);

//        if (TimeDimension.MINUTES.code.equals(apiIndexStatSearch.getDimension())) {
//            int minutes = apiIndexStatSearch.getTimeRange();//分钟数
//            long cur = System.currentTimeMillis();//时间戳
//            List<Long> times = new ArrayList<Long>(minutes);
//            for (int i=0; i<minutes; i++) {
//                times.add(Long.parseLong(DateUtil.dateFormat(new Date(cur - i * 60L * 1000L), "yyyyMMddHHmm")));
//            }
//            apiIndexStatSearch.setTimes(times);
//
//            if (apiIndexStatSearch.getTimes() == null || apiIndexStatSearch.getTimes().isEmpty()) {
//                throw new BaseException("时间参数为空");
//            }
//            return dataAccessService.getList(MAPPER + ".getMonitorByAppAndKeyAndMinutes", apiIndexStatSearch);
//        } else if (TimeDimension.HOUR.code.equals(apiIndexStatSearch.getDimension())) {
//            int hours = apiIndexStatSearch.getTimeRange();//小时数
//            long cur = System.currentTimeMillis();//时间戳
//            List<Long> times = new ArrayList<Long>(hours);
//            for (int i=0; i<hours; i++) {
//                times.add(Long.parseLong(DateUtil.dateFormat(new Date(cur - i * 60L * 60L * 1000L), "yyyyMMddHH")));
//            }
//            apiIndexStatSearch.setTimes(times);
//
//            if (apiIndexStatSearch.getTimes() == null || apiIndexStatSearch.getTimes().isEmpty()) {
//                throw new BaseException("时间参数为空");
//            }
//            return dataAccessService.getList(MAPPER + ".getMonitorByAppAndKeyAndMinutes", apiIndexStatSearch);
//        } else {
//            return null;
//        }

    }

}